<template>
	<view class="mainPage search" :style="{'padding-top': `${$store.state.safeArea.top || 0}px`, 'padding-bottom': `${$store.state.safeArea.bottom || 0}px`}">
		<public-head :title="title"></public-head>
		<!-- 搜索 begin -->
		<view class="box1">
			<u-field v-model.trim="changeValue" placeholder="请填写您所就职的医院名称" :focus="focuss" label="就职医院:" confirm-type="search" @input="input" @confirm="jump(changeValue)"></u-field>
		</view>
		<!-- 搜索 end -->
		<view class="box3" v-if="list.length">
			<view class="kong">
			</view>
			<view class="list">
				<view class="item" v-for="(item,index) in list" :key="index" @click="jump(item.keyword,item.htmlStr)">
					<rich-text :nodes="item.htmlStr"></rich-text>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
var that
var qingqiu = null
import publicHead from "@/components/publicHead/publicHead.vue"
export default {
	components: {
		publicHead
	},
	data() {
		return {
			title: "医院名称",
			recordUnfold: true,  //搜索历史是否展开
			focuss: true,        //input是否焦点
			changeValue: '',    //input的value的值
			recordList: [],      //搜索历史数据
			list: []
		}
	},
	watch: {
		changeValue() {
			if (that.changeValue == "") {
				that.list = []
			}
		}
	},
	onShow() {
		this.focuss = true
		console.log(that.list)
	},
	onHide() {
		this.focuss = false
	},
	onLoad() {
		that = this
		that.init()
	},
	methods: {
		init() {   //初始加载的方法
			this.loadOldKeyword();  //加载历史搜索
		},
		clearValue() {    //input 清空value
			this.changeValue = ""
		},
		confirm() {    //跳转搜索列表   点击完成按钮时触发
		},
		clearRecord() {  //清空搜索历史记录
			uni.showModal({
				content: '确定清除历史搜索记录？',
				success: function (res) {
					if (res.confirm) {
						console.log('用户点击确定');
						that.recordList = []
						uni.removeStorage({
							key: 'illnessSearchHistory'
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		unfold() {  //历史记录展开按钮
			this.recordUnfold = !this.recordUnfold
		},
		input(event) {
			that.list = []
			clearTimeout(qingqiu)
			if (that.changeValue == "") {
				return
			} else {
				qingqiu = setTimeout(() => {
					uni.request({
						url: 'https://ask.suoweilai.com/Petdoctor_vaguesearch_disease',
						method: "POST",
						data: {
							name: that.changeValue,
						},
						success: (res) => {
							that.list = []
							if (res.data.data) {
								that.list = that.drawCorrelativeKeyword(res.data.data, that.changeValue);
							} else {
								return
							}
						}
					});
				}, 1000)
			}
		},
		//加载历史搜索,自动读取本地Storage
		loadOldKeyword() {
			uni.getStorage({
				key: 'illnessSearchHistory',
				success: (res) => {
					var OldKeys = JSON.parse(res.data);
					that.recordList = OldKeys;
				}
			});
		},
		//高亮关键字
		drawCorrelativeKeyword(keywords, keyword) {
			var len = keywords.length,
				keywordArr = [];
			for (var i = 0; i < len; i++) {
				var row = keywords[i];
				// console.log(row,99899)
				// return
				//定义高亮#9f9f9f
				var html = row['name'].replace(keyword, "<span style='color: #FF902C;'>" + keyword + "</span>");
				html = '<div>' + html + '</div>';
				var tmpObj = {
					id: keywords[i].id,
					keyword: row['name'],
					htmlStr: html
				};
				// console.log(tmpObj,99899)
				keywordArr.push(tmpObj)
			}
			return keywordArr;
		},
		//执行搜索    //搜索按钮 和列表搜索  和历史记录搜索
		jump(keyword, htmlStr) {
			that.changeValue = keyword
			console.log('keyword', keyword, htmlStr)
		},
	}
}
</script>
<style scoped lang="scss">
.search {
	padding: 36rpx 0;
	box-sizing: border-box;
	.box1 {
		.fanhui {
			position: absolute;
			top: 50%;
			left: 32rpx;
			transform: translateY(-50%);
			image {
				display: inline-block;
				width: 18rpx;
				height: 33rpx;
			}
		}
		padding: 0 32rpx;
		position: relative;
		margin-bottom: 20rpx;
		.search-icon {
			position: absolute;
			width: 32rpx;
			height: 32rpx;
			top: 50%;
			transform: translateY(-50%);
			left: 70rpx;
		}
		input {
			border: 1px solid #c0c4cc;
			height: 72rpx;
			line-height: 72rpx;
			font-size: 24rpx;
			background: rgba(109, 114, 120, 0.11);
			border-radius: 34rpx;
			padding: 0 86rpx 0 96rpx;
			margin-right: 80rpx;
			border: none;
			// margin-left: 40rpx;
		}
		.close-circle {
			position: absolute;
			width: 36rpx;
			height: 36rpx;
			top: 50%;
			transform: translateY(-50%);
			right: 146rpx;
		}
		.cancel {
			display: inline-block;
			position: absolute;
			top: 50%;
			right: 32rpx;
			transform: translateY(-50%);
			font-size: 28rpx;
			font-weight: 400;
			color: #3b3535;
			line-height: 40px;
		}
	}
	.box2 {
		padding: 0 64rpx;
		.title {
			margin: 52rpx 0 36rpx;
			display: flex;
			justify-content: space-between;
			.name {
				font-size: 36rpx;
				font-weight: 500;
				color: #000000;
				line-height: 50rpx;
			}
			.icon {
				image {
					display: inline-block;
					height: 30rpx;
					width: 28rpx;
				}
			}
		}
		.list {
			// height: 250rpx;
			// overflow: hidden;
			.list1 {
				display: flex;
				flex-wrap: wrap;
				.item1 {
					display: inline-block;
					font-size: 24rpx;
					font-weight: 500;
					color: rgba(0, 0, 0, 0.66);
					line-height: 34rpx;
					background: #f7f7f5;
					border-radius: 8rpx;
					padding: 12rpx 32rpx;
					margin-right: 24rpx;
					margin-bottom: 34rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					image {
						display: inline-block;
						width: 32rpx;
						height: 32rpx;
					}
				}
			}
		}
	}
	.box3 {
		.kong {
			height: 20rpx;
			width: 100%;
			background-color: #f6f6f6;
		}
		.list {
			padding: 0 32rpx;
			.item {
				padding: 20rpx 24rpx;
				border-bottom: 2rpx solid rgba(0, 0, 0, 0.08);
			}
		}
	}
}
</style>
